﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SecuritiesAnalysis.Common;
using SecuritiesAnalysis.CurrentData;
using SecuritiesAnalysis.Interface;
using SecuritiesAnalysis.EXPMACalculator;
using System.IO;

using System.Windows.Forms.DataVisualization.Charting;


namespace SecuritiesAnalysis.ToolkitWinForm
{
    public partial class frmEXPMAView : Form
    {
        private SecuritiesAnalysisDataContext ctx;
        private List<decimal> expma1;
        private List<decimal> expma2;
        private List<DateTime> datelist;
        private List<T_Info_EXPMA> EXPMAlist;

        public frmEXPMAView()
        {
            InitializeComponent();
            ctx = new SecuritiesAnalysisDataContext();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //EXPMACalculator test = new EXPMACalculator();
            ICalculator expma = new EXPMACalculator.Calculator();
            expma.Calculate();
            //测试基本完成，但所有Calculator目前计算“all”的效率不高，疑似多次数据库连接造成

            MessageBox.Show("OK");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            databindXY();
        }

        private void databindXY()
        {
            EXPMAlist = ctx.T_Info_EXPMAs.Where(
                o => o.StockCode == (int)(this.numericStockCode.Value)
                )
                .OrderByDescending(c => c.Date)
                .Take((int)this.numericDays.Value)
                .ToList();

            this.datelist = new List<DateTime>();
            this.expma1 = new List<decimal>();
            this.expma2 = new List<decimal>();

            foreach (T_Info_EXPMA expma in EXPMAlist)
            {
                datelist.Add(expma.Date);
                expma1.Add(expma.EXPMA1);
                expma2.Add(expma.EXPMA2);
            }

            //string[] xValues = new string[7] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
            //double[] yValues = new double[7] { 50, 70, 40, 30, 20, 80, 10 };

            //chart1.ChartAreas["ChartArea1"].AxisY.IsStartedFromZero = false;

            //chart1.Series[1].IsValueShownAsLabel = true;
            //chart1.Series[1].ChartType = SeriesChartType.Stock;
            //chart1.ChartAreas["ChartArea1"].AxisY.Interval = 0.01;

            chart1.Series["Series1"].Points.DataBindXY(datelist, expma1);
            chart1.Series["Series2"].Points.DataBindXY(datelist, expma2);
        }

        private void chart1_CursorPositionChanging(object sender, CursorEventArgs e)
        {
            SetPosition(e.Axis, e.NewPosition);
        }

        private void chart1_CursorPositionChanged(object sender, CursorEventArgs e)
        {
            SetPosition(e.Axis, e.NewPosition);
        }

        // Set Cursor Position to display control.
        private void SetPosition(Axis axis, double position)
        {
            if (double.IsNaN(position))
                return;

            StringBuilder cursorinfo = new StringBuilder();

            if (axis.AxisName == AxisName.X)
            {
                // Convert Double to DateTime.
                DateTime dateTimeX = DateTime.FromOADate(position);
                cursorinfo.Append("日期：");
                cursorinfo.Append(dateTimeX.ToShortDateString());

                T_Info_EXPMA currentma = this.EXPMAlist.Where(o => o.Date == dateTimeX).FirstOrDefault();
                if (currentma == null)
                {
                    cursorinfo.Append("  休市");
                    chart1.Titles[0].Text = cursorinfo.ToString();
                }
                else
                {
                    cursorinfo.Append("  EXPMA1：");
                    cursorinfo.Append(currentma.EXPMA1.ToString());
                    cursorinfo.Append("  EXPMA2：");
                    cursorinfo.Append(currentma.EXPMA2.ToString());

                    chart1.Titles[0].Text = cursorinfo.ToString();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {

        }


    }
}
